// pages/edit/edit.js
const {detail, option, update, add, list, follow, faceMatch, session, rubbish, levelOption, baiduIdentify} = require("../../api/index.js")
const des = require('../../utils/des.js')
const utils = require("../../utils/index.js")
Page({

/**
* 页面的初始数据
*/data: {
imgIcon: "../../static/upload.png",
editStatus: false,
baseURL:'',
sessionReadArr:[],
detailList: null,
id: "",
cross:"",
ruleForm:{},
userid:getApp().globalData.userInfo.id,
userInfo:getApp().globalData.userInfo,
ro:{
},
daoshigonghao:"",
mima:"",
daoshixingming:"",
touxiang:"",
lianxidianhua:"",
xingbieList:"男,女".split(','),
xingbieIndex:0,
zhicheng:"",
xiangmufenleiIndex: 0,
xiangmufenleiList: [],
leixingList:"校内,校外".split(','),
leixingIndex:0,
zhuanyejingyan:"",
zhidaojingyan:"",
xueshubeijing:"",
suozaidanwei:"",
youxiang:"",
suozaiyuanxi:"",
gerenjianjie:"",
lingyuzhuanzhang:"",
xiangmuleixing:"",
zhidaoguoxiangmu:"",
gongzuojingli:"",
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(options) {
let userid
if (options?.id) {
this.setData({
editStatus: true
})
}
let nowTable = wx.getStorageSync("nowTable");
const res = await session(nowTable)
if(res.data.code==0){
getApp().globalData.userInfo=res?.data
userid = res?.data.id
this.setData({
userid
})
}
let baseURL =wx.getStorageSync('baseURL') + '/'
const id = getApp().globalData.detailId
this.setData({
refid:id,
baseURL
})
//人脸识别
 let xingbieList="男,女".split(',');
 let xingbieIndex=0;
 this.setData({
   xingbieList,
   xingbieIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
const xiangmufenleires = await option('xiangmufenlei/xiangmufenlei')
xiangmufenleires.data.unshift('请选择项目分类')
this.setData({
xiangmufenleiList: xiangmufenleires.data
})
 let leixingList="校内,校外".split(',');
 let leixingIndex=0;
 this.setData({
   leixingList,
   leixingIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
let  ro=this.data.ro;
if(options?.cross){
var obj = wx.getStorageSync('crossObj');
let refobjempty={}
for (var o in obj){
if(o=='daoshigonghao'){
   refobjempty["daoshigonghao"]=obj[o]
ro.daoshigonghao = true;
continue;
}else{
}
if(o=='mima'){
   refobjempty["mima"]=obj[o]
ro.mima = true;
continue;
}else{
}
if(o=='daoshixingming'){
   refobjempty["daoshixingming"]=obj[o]
ro.daoshixingming = true;
continue;
}else{
}
if(o=='touxiang'){
refobjempty['touxiang']=obj[o];
refobjempty['touxiangPath']=obj[o].split(",")[0];
ro.touxiang = true;
continue;
}else{
}
if(o=='lianxidianhua'){
   refobjempty["lianxidianhua"]=obj[o]
ro.lianxidianhua = true;
continue;
}else{
}
if(o=='xingbie'){
   refobjempty["xingbie"]=obj[o]
ro.xingbie = true;
continue;
}else{
}
if(o=='zhicheng'){
   refobjempty["zhicheng"]=obj[o]
ro.zhicheng = true;
continue;
}else{
}
if(o=='xiangmufenlei'){
   refobjempty["xiangmufenlei"]=obj[o]
ro.xiangmufenlei = true;
continue;
}else{
}
if(o=='leixing'){
   refobjempty["leixing"]=obj[o]
ro.leixing = true;
continue;
}else{
}
if(o=='zhuanyejingyan'){
   refobjempty["zhuanyejingyan"]=obj[o]
ro.zhuanyejingyan = true;
continue;
}else{
}
if(o=='zhidaojingyan'){
   refobjempty["zhidaojingyan"]=obj[o]
ro.zhidaojingyan = true;
continue;
}else{
}
if(o=='xueshubeijing'){
   refobjempty["xueshubeijing"]=obj[o]
ro.xueshubeijing = true;
continue;
}else{
}
if(o=='suozaidanwei'){
   refobjempty["suozaidanwei"]=obj[o]
ro.suozaidanwei = true;
continue;
}else{
}
if(o=='youxiang'){
   refobjempty["youxiang"]=obj[o]
ro.youxiang = true;
continue;
}else{
}
if(o=='suozaiyuanxi'){
   refobjempty["suozaiyuanxi"]=obj[o]
ro.suozaiyuanxi = true;
continue;
}else{
}
if(o=='gerenjianjie'){
   refobjempty["gerenjianjie"]=obj[o]
ro.gerenjianjie = true;
continue;
}else{
}
if(o=='lingyuzhuanzhang'){
   refobjempty["lingyuzhuanzhang"]=obj[o]
ro.lingyuzhuanzhang = true;
continue;
}else{
}
if(o=='xiangmuleixing'){
   refobjempty["xiangmuleixing"]=obj[o]
ro.xiangmuleixing = true;
continue;
}else{
}
if(o=='zhidaoguoxiangmu'){
   refobjempty["zhidaoguoxiangmu"]=obj[o]
ro.zhidaoguoxiangmu = true;
continue;
}else{
}
if(o=='gongzuojingli'){
   refobjempty["gongzuojingli"]=obj[o]
ro.gongzuojingli = true;
continue;
}else{
}
}
let  statusColumnName=wx.getStorageSync('statusColumnName');
statusColumnName=statusColumnName.replace('[',"").replace(']',"");
this.setData({
ro,
cross:options?.cross,
statusColumnName
})
this.setData(refobjempty)
}
if(id){
// 如果上一级页面传递了id，获取改id数据信息
const data=getApp().globalData.detailList;
let dataEmpty={};
const  def_6= 0  ;
this.data.xingbieList.map((v, index) => {
if (v == data.xingbie|| (v == def_6 && def_6 !=null)) {
 this.setData({
   xingbieIndex: index,
   xingbie: v,
 })
}else if (this.data.xingbieList.length == 1) {
 this.setData({
   xingbieIndex: 0,
   xingbie: v,
 })
}
})
xiangmufenleires.data.map((item, index) => {
if (item == data.xiangmufenlei) {
 this.setData({
xiangmufenleiIndex: index,
xiangmufenlei: item
 })
}else if (this.data.xiangmufenleiList.length == 1) {
this.setData({
xiangmufenleiIndex: 0,
xiangmufenlei: v
 })
}
})
const  def_9= 0  ;
this.data.leixingList.map((v, index) => {
if (v == data.leixing|| (v == def_9 && def_9 !=null)) {
 this.setData({
   leixingIndex: index,
   leixing: v,
 })
}else if (this.data.leixingList.length == 1) {
 this.setData({
   leixingIndex: 0,
   leixing: v,
 })
}
})
for (var o in data){
if(o=='daoshigonghao') {
 if(!this.data.daoshigonghao){
  dataEmpty["daoshigonghao"] = data[o]
 }
}
if(o=='mima') {
 if(!this.data.mima){
  dataEmpty["mima"] = data[o]
 }
}
if(o=='daoshixingming') {
 if(!this.data.daoshixingming){
  dataEmpty["daoshixingming"] = data[o]
 }
}
if(o=='touxiang') {
dataEmpty['touxiang'] = data[o];
dataEmpty['touxiangPath'] = data[o].split(",")[0];
}
if(o=='lianxidianhua') {
 if(!this.data.lianxidianhua){
  dataEmpty["lianxidianhua"] = data[o]
 }
}
if(o=='xingbie') {
 if(!this.data.xingbie){
  dataEmpty["xingbie"] = data[o]
 }
}
if(o=='zhicheng') {
 if(!this.data.zhicheng){
  dataEmpty["zhicheng"] = data[o]
 }
}
if(o=='xiangmufenlei') {
 if(!this.data.xiangmufenlei){
  dataEmpty["xiangmufenlei"] = data[o]
 }
}
if(o=='leixing') {
 if(!this.data.leixing){
  dataEmpty["leixing"] = data[o]
 }
}
if(o=='zhuanyejingyan') {
 if(!this.data.zhuanyejingyan){
  dataEmpty["zhuanyejingyan"] = data[o]
 }
}
if(o=='zhidaojingyan') {
 if(!this.data.zhidaojingyan){
  dataEmpty["zhidaojingyan"] = data[o]
 }
}
if(o=='xueshubeijing') {
 if(!this.data.xueshubeijing){
  dataEmpty["xueshubeijing"] = data[o]
 }
}
if(o=='suozaidanwei') {
 if(!this.data.suozaidanwei){
  dataEmpty["suozaidanwei"] = data[o]
 }
}
if(o=='youxiang') {
 if(!this.data.youxiang){
  dataEmpty["youxiang"] = data[o]
 }
}
if(o=='suozaiyuanxi') {
 if(!this.data.suozaiyuanxi){
  dataEmpty["suozaiyuanxi"] = data[o]
 }
}
if(o=='gerenjianjie') {
 if(!this.data.gerenjianjie){
  dataEmpty["gerenjianjie"] = data[o]
 }
}
if(o=='lingyuzhuanzhang') {
 if(!this.data.lingyuzhuanzhang){
  dataEmpty["lingyuzhuanzhang"] = data[o]
 }
}
if(o=='xiangmuleixing') {
 if(!this.data.xiangmuleixing){
  dataEmpty["xiangmuleixing"] = data[o]
 }
}
if(o=='zhidaoguoxiangmu') {
 if(!this.data.zhidaoguoxiangmu){
  dataEmpty["zhidaoguoxiangmu"] = data[o]
 }
}
if(o=='gongzuojingli') {
 if(!this.data.gongzuojingli){
  dataEmpty["gongzuojingli"] = data[o]
 }
}
}
this.setData(dataEmpty)
}else {
this.setData({
})
}
// ss读取
let sessionReadArr=[]
this.setData({
cross:options?.cross,
ro,
id,
})

if (wx.getStorageSync('raffleType') ) {
 wx.removeStorageSync('raffleType')
 setTimeout(() => {
  this.submit()
 }, 300)
}




















},
getUUID () {
return new Date().getTime();
},
onUnload: function () {
},
onShow() {
},
 commentScore(e){
 if(this.data?.commentScore){
  this.setData({
   commentScore:e.detail
  })
 }
 },






//xingbie, 0
// 下拉变化
xingbieChange(e) {
this.setData({
 xingbieIndex:   e.detail.value,
 xingbie :this.data.xingbieList[e.detail.value]
})
},

//xiangmufenlei, 0
// 下拉变化
xiangmufenleiChange(e) {
this.setData({
 xiangmufenleiIndex:   e.detail.value,
 xiangmufenlei :this.data.xiangmufenleiList[e.detail.value]
})
},

//leixing, 0
// 下拉变化
leixingChange(e) {
this.setData({
 leixingIndex:   e.detail.value,
 leixing :this.data.leixingList[e.detail.value]
})
},














uploadtouxiang() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
 touxiangPath: tempFilePaths[0]
})
let _this = this;
// 上传网络图片
const baseURL = wx.getStorageSync('baseURL')
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
 'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
 let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
 if (result.code == 0) {
  this.setData({
    touxiang: 'file/' + result.file
  })
 } else {
  wx.showToast({
   title: result.msg,
   icon: 'none',
   duration: 2000
  });
 }
}
})

this.setData({
face: tempFilePaths[0]
});

}
})
},






































































async submit() {
let that = this
var query = wx.createSelectorQuery();


const baseURL = wx.getStorageSync('baseURL') + "/"
const regex = new RegExp(baseURL, "g");
const obj={
daoshigonghao: this.data.daoshigonghao,
daoshixingming: this.data.daoshixingming,
touxiang:this.data.touxiang?.replace(regex, ""),
lianxidianhua: this.data.lianxidianhua,
xingbie:this.data.xingbieList[this.data.xingbieIndex]?this.data.xingbieList[this.data.xingbieIndex]:'',
zhicheng: this.data.zhicheng,
xiangmufenlei:this.data.xiangmufenleiList[this.data.xiangmufenleiIndex]?this.data.xiangmufenleiList[this.data.xiangmufenleiIndex]:'',
leixing:this.data.leixingList[this.data.leixingIndex]?this.data.leixingList[this.data.leixingIndex]:'',
zhuanyejingyan: this.data.zhuanyejingyan,
zhidaojingyan: this.data.zhidaojingyan,
xueshubeijing: this.data.xueshubeijing,
suozaidanwei: this.data.suozaidanwei,
youxiang: this.data.youxiang,
suozaiyuanxi: this.data.suozaiyuanxi,
gerenjianjie: this.data.gerenjianjie,
lingyuzhuanzhang: this.data.lingyuzhuanzhang,
xiangmuleixing: this.data.xiangmuleixing,
zhidaoguoxiangmu: this.data.zhidaoguoxiangmu,
gongzuojingli: this.data.gongzuojingli,
}
const detailId= getApp().globalData.detailId
const tableName= `daoshi`
//跨表计算判断
var obj2;
var  ruleForm=obj
obj2 = ruleForm
this.data.refid==""? ruleForm['refid']= getApp().globalData.detailId:""
ruleForm['userid']=getApp().globalData.userInfo.id;
var userInfo=getApp().globalData.userInfo
const sessionReadArr=this.data.sessionReadArr
const phonePattern = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
const mobilePattern = /^(?:\+?86)?1[3-9]\d{9}$/;
const emailPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
const idPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dxX]$/;
const urlPattern = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)+[/#?]?.*$/;
 if(!this.data.daoshigonghao ){
  wx.showToast({
   icon: "none",
   title: `导师工号不能为空`,
  })
  return
 }





 if(!this.data.mima ){
  wx.showToast({
   icon: "none",
   title: `密码不能为空`,
  })
  return
 }





 if(!this.data.daoshixingming ){
  wx.showToast({
   icon: "none",
   title: `导师姓名不能为空`,
  })
  return
 }









 if(!this.data.lianxidianhua ){
  wx.showToast({
   icon: "none",
   title: `联系电话不能为空`,
  })
  return
 }


const valuelianxidianhua = this.data.lianxidianhua;

if (!mobilePattern.test(valuelianxidianhua)) {
wx.showToast({
icon: "none",
title: `联系电话应输入手机格式`,
})
return
}







 if(!this.data.zhicheng ){
  wx.showToast({
   icon: "none",
   title: `职称不能为空`,
  })
  return
 }





 if(!this.data.xiangmufenlei ){
  wx.showToast({
   icon: "none",
   title: `项目分类不能为空`,
  })
  return
 }





 if(!this.data.leixing ){
  wx.showToast({
   icon: "none",
   title: `类型不能为空`,
  })
  return
 }





 if(!this.data.zhuanyejingyan ){
  wx.showToast({
   icon: "none",
   title: `专业经验不能为空`,
  })
  return
 }





 if(!this.data.zhidaojingyan ){
  wx.showToast({
   icon: "none",
   title: `指导经验不能为空`,
  })
  return
 }





 if(!this.data.xueshubeijing ){
  wx.showToast({
   icon: "none",
   title: `学术背景不能为空`,
  })
  return
 }





 if(!this.data.suozaidanwei ){
  wx.showToast({
   icon: "none",
   title: `所在单位不能为空`,
  })
  return
 }





 if(!this.data.youxiang ){
  wx.showToast({
   icon: "none",
   title: `邮箱不能为空`,
  })
  return
 }


const valueyouxiang = this.data.youxiang;

if (!emailPattern.test(valueyouxiang)) {
wx.showToast({
icon: "none",
title: `邮箱应输入邮件格式`,
})
return
}



 if(!this.data.suozaiyuanxi ){
  wx.showToast({
   icon: "none",
   title: `所在院系不能为空`,
  })
  return
 }









 if(!this.data.lingyuzhuanzhang ){
  wx.showToast({
   icon: "none",
   title: `领域专长不能为空`,
  })
  return
 }





 if(!this.data.xiangmuleixing ){
  wx.showToast({
   icon: "none",
   title: `可指导项目类型不能为空`,
  })
  return
 }













//更新跨表属性
var crossuserid;
var crossrefid;
var crossoptnum;
if(this.data.cross) {
wx.setStorageSync('crossCleanType', true);
var statusColumnName = wx.getStorageSync('statusColumnName');
var statusColumnValue = wx.getStorageSync('statusColumnValue');
if (statusColumnName != '') {
obj2 = wx.getStorageSync('crossObj');
if (!statusColumnName.startsWith("[")) {
for (var o in obj2) {
if (statusColumnName==o){
obj2[o] = statusColumnValue;
}

}
var table = wx.getStorageSync('crossTable');

await update(table, obj2)
} else {

crossuserid =getApp().globalData.userInfo.id
crossrefid =  this.data.id
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.replace(/\[/, "").replace(/\]/, "");
}
}
}
 ruleForm?.shhf?ruleForm.shhf=this.data.shhf:''
if(crossrefid && crossuserid) {
ruleForm['crossuserid'] =getApp().globalData.userInfo.id;
ruleForm['crossrefid'] =obj2.id;
this.setData({
ruleForm
})
let params = {
page: 1,
limit: 10,
crossuserid: crossuserid,
crossrefid: crossrefid,
}
const tips = wx.getStorageSync('tips')
let corssRes = await list(`daoshi`, params)
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.match(/\d+/g);
if (corssRes?.data?.total >= parseInt(crossoptnum)) {
wx.showToast({
icon: "none",
title: tips,
})
wx.removeStorageSync('crossCleanType');
return ;
}
else {
//跨表计算
if (ruleForm.id ) {
await update(`daoshi`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
 await add(`daoshi`, ruleForm)
}
}
}
else {
//跨表计算
if (ruleForm.id || this.data.editStatus) {
this.data.editStatus?ruleForm['id']= getApp().globalData.detailId:""
await update(`daoshi`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
await add(`daoshi`, ruleForm)
}
}
getApp().globalData.editorContent=''
wx.showToast({
title: '提交成功',
icon: "none"
})
const preId = getApp().globalData.detailId

if(table){
let res = await detail(table, preId)
if(res.code==0){
getApp().globalData.detailList = res.data
}

}
wx.navigateBack({
delta: 1,
complete: () => {
// 触发事件通知，传递需要更新的数据
const pages = getCurrentPages();
if (pages.length >= 1) {
const prePage = pages[pages.length - 1];
prePage.onLoad(); //
}
}
})

},
onHide() {

},


/**
* 生命周期函数--监听页面卸载
*/
onUnload() {

},


/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage() {

}
})